package com.lin.missyou.api.v1;


import com.lin.missyou.bo.PageCounter;
import com.lin.missyou.core.LocalUser;
import com.lin.missyou.core.interceptors.ScopeLevel;
import com.lin.missyou.dto.OrderDTO;
import com.lin.missyou.exception.http.ParameterException;
import com.lin.missyou.logic.OrderChecker;
import com.lin.missyou.model.Order;
import com.lin.missyou.service.OrderService;
import com.lin.missyou.util.CommonUtil;
import com.lin.missyou.vo.OrderIdVO;
import com.lin.missyou.vo.OrderPureVO;
import com.lin.missyou.vo.OrderSimplifyVO;
import com.lin.missyou.vo.PagingDozer;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("order")
@Validated
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Value("${missyou.order.pay-time-limit}")
    private Long payTimeLimit;

    @PostMapping("")
    @ScopeLevel
    public OrderIdVO placeOrder(@RequestBody OrderDTO orderDTO) {
        Long uid = LocalUser.getUser().getId();
        OrderChecker orderChecker = orderService.isOk(uid, orderDTO);
        Long orderId = this.orderService.placeOrder(uid, orderDTO, orderChecker);
        return new OrderIdVO(orderId);
    }

    @GetMapping("status/unpaid")
    @ScopeLevel
    public PagingDozer<Order, OrderSimplifyVO> getUnpaid(@RequestParam(value = "start", defaultValue = "0") Integer start,
                                                         @RequestParam(value = "count", defaultValue = "10") Integer count) {
        PageCounter pageCounter = CommonUtil.convertToPageParameter(start, count);

        Page<Order> orderPage = this.orderService.getUnpaid(pageCounter.getPage(), pageCounter.getCount());
        PagingDozer<Order, OrderSimplifyVO> page = new PagingDozer<>(orderPage, OrderSimplifyVO.class);
        page.getItems().forEach(o -> {
            ((OrderSimplifyVO) o).setPeriod(this.payTimeLimit);
        });
        return page;
    }


    @GetMapping("/by/status/{status}")
    @ScopeLevel
    public PagingDozer<Order, OrderSimplifyVO> getByStatus(@PathVariable Integer status,
                                                           @RequestParam(value = "start", defaultValue = "0") Integer start,
                                                           @RequestParam(value = "count", defaultValue = "10") Integer count) {
        PageCounter pageCounter = CommonUtil.convertToPageParameter(start, count);
        Page<Order> orderPage = this.orderService.getByStatus(status, pageCounter.getPage(), pageCounter.getCount());
        PagingDozer<Order, OrderSimplifyVO> pagingDozer = new PagingDozer<>(orderPage, OrderSimplifyVO.class);
        pagingDozer.getItems().forEach(o -> {
            ((OrderSimplifyVO) o).setPeriod(this.payTimeLimit);
        });
        return pagingDozer;
    }

    @GetMapping("/detail/{id}")
    @ScopeLevel
    public OrderPureVO getOrderDetail(@PathVariable Long id){
        Long uid = LocalUser.getUser().getId();
        Order order = this.orderService.getOrderDetail(uid,id)
                .orElseThrow(()->new ParameterException(50009));
        return new OrderPureVO(order, payTimeLimit);
    }
}
